package com.pascal91.webpass.controller;

import com.artezio.buyndrive.domain.Admin;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class MainController {

    @RequestMapping("/")
    public ModelAndView begin(Model model) {
        return new ModelAndView("redirect:/home.html");
    }

    @RequestMapping("/home.html")
    public ModelAndView home(Model model) {
        return new ModelAndView("home");
    }

    @RequestMapping(value = "/send", method = RequestMethod.POST)
    public ModelAndView send(Model model, HttpServletRequest servletRequest, HttpSession session) {

        ModelAndView mv = new ModelAndView("home");

        String message = servletRequest.getParameter("message");

        System.out.println(message);

        if (session.getAttribute("text") != null){
            String sessionText = String.valueOf(session.getAttribute("text"));
            session.setAttribute("text", sessionText.concat(message).concat("\n"));
            mv.addObject("sessionText", sessionText.concat(message).concat("\n"));
        }else{
            session.setAttribute("text", message);
            mv.addObject("sessionText", message);
        }

        return mv;
    }

    @RequestMapping("/clear")
    public ModelAndView clear(Model model, HttpSession session) {

        session.invalidate();

        return new ModelAndView("home");
    }
}